<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>金额转换工具</title>
    <style>
        .error { color: red; }
        .result { margin-top: 10px; }
    </style>
</head>
<body>
    <input type="text" id="amount" placeholder="请输入金额">
    <button onclick="process()">转换</button>
    <span id="error" class="error"></span>
    <div class="result">银行小写：<span id="lowercase"></span></div>
    <div class="result">大写金额：<span id="uppercase"></span></div>

    <script>
        function validate(input) {
            if (!input.trim()) return '金额不能为空';
            if (!/^\d{1,13}(\.\d+)?$/.test(input)) return '格式错误：整数1-13位，小数可选但至少1位';
            
            const [intPart] = input.split('.');
            const processedInt = intPart.replace(/^0+/, '') || '0';
            if (processedInt.length > 13) return '整数部分不能超过13位';
            return null;
        }

        function processAmount(input) {
            let [integer, decimal = '00'] = input.split('.');
            integer = integer.replace(/^0+/, '') || '0';
            decimal = decimal.slice(0, 2).padEnd(2, '0');
            return { integer, decimal };
        }

        function formatCurrency(numStr) {
            return numStr.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
        }

        function toChineseUpper(processed) {
            const digits = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'];
            const units = ['','拾','佰','仟'];
            const bigUnits = ['','万','亿'];
            
            let result = '';
            let intPart = processed.integer;
            let chunkCount = 0;

            while(intPart.length > 0) {
                let chunk = intPart.slice(-4).split('').reverse();
                intPart = intPart.slice(0, -4);
                let chunkStr = '';
                
                chunk.forEach((n, i) => {
                    if(n !== '0') {
                        chunkStr = digits[n] + units[i] + chunkStr;
                    } else if(chunkStr[0] !== '零') {
                        chunkStr = '零' + chunkStr;
                    }
                });

                chunkStr = chunkStr.replace(/零+$/, '');
                if(chunkStr) {
                    result = chunkStr + bigUnits[chunkCount] + result;
                }
                chunkCount++;
            }

            result = result.replace(/零+/g, '零').replace(/^零|零$/g, '') || '零';
            return result + '元';
        }

        function process() {
            const input = document.getElementById('amount').value;
            const errorSpan = document.getElementById('error');
            errorSpan.textContent = '';

            // 输入校验
            const error = validate(input);
            if(error) {
                errorSpan.textContent = error;
                return;
            }

            // 金额处理
            const processed = processAmount(input);
            
            // 银行小写格式
            const formattedInt = formatCurrency(processed.integer);
            document.getElementById('lowercase').textContent = 
                `${formattedInt}.${processed.decimal}`;

            // 大写金额转换
            let upperResult = toChineseUpper(processed);
            const digits = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'];
            
            // 处理小数部分
            const [jiao, fen] = processed.decimal.split('');
            let decimalPart = '';
            if(jiao !== '0') decimalPart += digits[jiao] + '角';
            if(fen !== '0') decimalPart += digits[fen] + '分';
            
            upperResult += decimalPart || '整';
            upperResult = upperResult.replace(/零+/g, '零')
                                   .replace(/零元/, '元')
                                   .replace(/零角零分|零$/, '');

            document.getElementById('uppercase').textContent = upperResult || '零元整';
        }
    </script>
</body>
</html>